package xyf.p02.stack_queue;

/**
 * ClassName: M0301_Three4One
 * Description:
 * Author: xyf
 * Date: 9/3/22 6:22 PM
 * Version: 1.0
 **/
public class M0301_Three4One
{
    class TripleInOne
    {
        private int[] nums;

        private int[] indexs;

        public TripleInOne(int stackSize)
        {
            nums = new int[stackSize * 3];
            indexs = new int[3];
            indexs[0] = -3;
            indexs[1] = -2;
            indexs[2] = -1;
        }

        public void push(int stackNum, int value)
        {
            int index = indexs[stackNum];
            index += 3;
            if (index > nums.length - 1)
            {
                return;
            }
            nums[index] = value;
            indexs[stackNum] = index;
        }

        public int pop(int stackNum)
        {
            int index = indexs[stackNum];
            if (index < 0)
            {
                return -1;
            }

            int num = nums[index];

            nums[index] = -1;
            indexs[stackNum] -= 3;
            return num;
        }

        public int peek(int stackNum)
        {
            int index = indexs[stackNum];
            if (index < 0)
            {
                return -1;
            }

            int num = nums[index];
            return num;
        }

        public boolean isEmpty(int stackNum)
        {
            return indexs[stackNum] < 0;
        }

    }
}
